package com.huawei.hwid.core.d.a;

import android.content.Context;
import android.text.TextUtils;
import android.util.Xml;
import com.huawei.hwid.core.c.a;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.g;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.encrypt.c;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class d implements a {
    private static ArrayList<HwAccount> a(String str, Context context, boolean z) {
        ArrayList<HwAccount> a;
        Throwable th;
        InputStream inputStream = null;
        InputStream inputStream2 = null;
        try {
            File file = new File(context.getFilesDir().getAbsolutePath() + "/" + str);
            if (file.exists()) {
                inputStream2 = new FileInputStream(file);
                try {
                    XmlPullParser newPullParser = Xml.newPullParser();
                    newPullParser.setInput(inputStream2, null);
                    a = a(newPullParser, context, z);
                    if (inputStream2 != null) {
                        try {
                            inputStream2.close();
                        } catch (IOException e) {
                            e.d("XmlFileGrade", "IOException", true);
                        }
                    }
                } catch (XmlPullParserException e2) {
                    inputStream = inputStream2;
                    try {
                        e.d("XmlFileGrade", "XmlPullParserException readAccountsFromXmlfailed!", true);
                        a = new ArrayList();
                        if (inputStream != null) {
                            try {
                                inputStream.close();
                            } catch (IOException e3) {
                                e.d("XmlFileGrade", "IOException", true);
                            }
                        }
                        return a;
                    } catch (Throwable th2) {
                        th = th2;
                        if (inputStream != null) {
                            try {
                                inputStream.close();
                            } catch (IOException e4) {
                                e.d("XmlFileGrade", "IOException", true);
                            }
                        }
                        throw th;
                    }
                } catch (FileNotFoundException e5) {
                    inputStream = inputStream2;
                    e.d("XmlFileGrade", "readAccountsFromXml FileNotFoundException", true);
                    a = new ArrayList();
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e6) {
                            e.d("XmlFileGrade", "IOException", true);
                        }
                    }
                    return a;
                } catch (IOException e7) {
                    inputStream = inputStream2;
                    e.d("XmlFileGrade", "readAccountsFromXml IOException", true);
                    a = new ArrayList();
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e8) {
                            e.d("XmlFileGrade", "IOException", true);
                        }
                    }
                    return a;
                } catch (RuntimeException e9) {
                    inputStream = inputStream2;
                    e.d("XmlFileGrade", "readAccountsFromXml RuntimeException", true);
                    a = new ArrayList();
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e10) {
                            e.d("XmlFileGrade", "IOException", true);
                        }
                    }
                    return a;
                } catch (Exception e11) {
                    inputStream = inputStream2;
                    e.d("XmlFileGrade", "readAccountsFromXml Exception", true);
                    a = new ArrayList();
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e12) {
                            e.d("XmlFileGrade", "IOException", true);
                        }
                    }
                    return a;
                } catch (Throwable th3) {
                    th = th3;
                    inputStream = inputStream2;
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    throw th;
                }
            }
            e.a("XmlFileGrade", "xmlfilegradle sdk filepath not exist", true);
            a = new ArrayList();
            if (null != null) {
                try {
                    inputStream2.close();
                } catch (IOException e13) {
                    e.d("XmlFileGrade", "IOException", true);
                }
            }
        } catch (XmlPullParserException e14) {
            e.d("XmlFileGrade", "XmlPullParserException readAccountsFromXmlfailed!", true);
            a = new ArrayList();
            if (inputStream != null) {
                inputStream.close();
            }
            return a;
        } catch (FileNotFoundException e15) {
            e.d("XmlFileGrade", "readAccountsFromXml FileNotFoundException", true);
            a = new ArrayList();
            if (inputStream != null) {
                inputStream.close();
            }
            return a;
        } catch (IOException e16) {
            e.d("XmlFileGrade", "readAccountsFromXml IOException", true);
            a = new ArrayList();
            if (inputStream != null) {
                inputStream.close();
            }
            return a;
        } catch (RuntimeException e17) {
            e.d("XmlFileGrade", "readAccountsFromXml RuntimeException", true);
            a = new ArrayList();
            if (inputStream != null) {
                inputStream.close();
            }
            return a;
        } catch (Exception e18) {
            e.d("XmlFileGrade", "readAccountsFromXml Exception", true);
            a = new ArrayList();
            if (inputStream != null) {
                inputStream.close();
            }
            return a;
        }
        return a;
    }

    private static void a(XmlPullParser xmlPullParser, Context context, boolean z, HwAccount hwAccount, String str) throws XmlPullParserException, IOException {
        String nextText;
        if ("userId".equals(str)) {
            nextText = xmlPullParser.nextText();
            if (z) {
                nextText = c.a(context, nextText);
            }
            hwAccount.d(nextText);
        } else if ("deviceId".equals(str)) {
            nextText = xmlPullParser.nextText();
            if (z) {
                nextText = c.a(context, nextText);
            }
            hwAccount.h(nextText);
        } else if ("subDeviceId".equals(str)) {
            nextText = xmlPullParser.nextText();
            if (z) {
                nextText = c.a(context, nextText);
            }
            hwAccount.i(nextText);
        } else if ("deviceType".equals(str)) {
            hwAccount.j(xmlPullParser.nextText());
        } else if ("serviceToken".equals(str)) {
            hwAccount.f(c.a(context, xmlPullParser.nextText()));
        } else if ("siteId".equals(str)) {
            try {
                hwAccount.a(Integer.parseInt(xmlPullParser.nextText()));
            } catch (NumberFormatException e) {
                e.c("XmlFileGrade", "NumberFormatException: read accounts.xml parseInt error", true);
            } catch (Exception e2) {
                e.c("XmlFileGrade", "read accounts.xml parseInt error", true);
            }
        } else if ("accountType".equals(str)) {
            hwAccount.g(xmlPullParser.nextText());
        } else if ("loginUserName".equals(str)) {
            hwAccount.k(c.a(context, xmlPullParser.nextText()));
        } else if ("countryIsoCode".equals(str)) {
            hwAccount.a(c.a(context, xmlPullParser.nextText()));
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static java.util.ArrayList<com.huawei.hwid.core.datatype.HwAccount> a(org.xmlpull.v1.XmlPullParser r6, android.content.Context r7, boolean r8) throws org.xmlpull.v1.XmlPullParserException, java.io.IOException {
        /*
        r4 = 1;
        r0 = "XmlFileGrade";
        r1 = "parseAccountsFromXml Start";
        com.huawei.hwid.core.d.b.e.e(r0, r1, r4);
        r0 = r6.getEventType();
        r2 = new java.util.ArrayList;
        r2.<init>();
        r1 = new com.huawei.hwid.core.datatype.HwAccount;
        r1.<init>();
    L_0x0016:
        if (r4 == r0) goto L_0x008b;
    L_0x0018:
        r3 = r6.getName();
        switch(r0) {
            case 0: goto L_0x002f;
            case 1: goto L_0x001f;
            case 2: goto L_0x0031;
            case 3: goto L_0x0073;
            default: goto L_0x001f;
        };
    L_0x001f:
        r0 = "XmlFileGrade";
        r3 = "default";
        com.huawei.hwid.core.d.b.e.e(r0, r3, r4);
    L_0x0026:
        r0 = r1;
    L_0x0027:
        r1 = r6.next();
        r5 = r0;
        r0 = r1;
        r1 = r5;
        goto L_0x0016;
    L_0x002f:
        r0 = r1;
        goto L_0x0027;
    L_0x0031:
        r0 = "account";
        r0 = r0.equals(r3);
        if (r0 == 0) goto L_0x0057;
    L_0x0039:
        r0 = "";
        r3 = "appId";
        r0 = r6.getAttributeValue(r0, r3);
        r3 = android.text.TextUtils.isEmpty(r0);
        if (r3 == 0) goto L_0x0052;
    L_0x0047:
        r0 = "XmlFileGrade";
        r3 = "authTokenType is null";
        com.huawei.hwid.core.d.b.e.e(r0, r3, r4);
        r0 = com.huawei.hwid.core.d.b.c(r7);
    L_0x0052:
        r1.c(r0);
        r0 = r1;
        goto L_0x0027;
    L_0x0057:
        r0 = "accountName";
        r0 = r0.equals(r3);
        if (r0 == 0) goto L_0x006e;
    L_0x005f:
        r0 = r6.nextText();
        if (r8 == 0) goto L_0x0069;
    L_0x0065:
        r0 = com.huawei.hwid.core.encrypt.c.a(r7, r0);
    L_0x0069:
        r1.b(r0);
        r0 = r1;
        goto L_0x0027;
    L_0x006e:
        a(r6, r7, r8, r1, r3);
        r0 = r1;
        goto L_0x0027;
    L_0x0073:
        r0 = "account";
        r0 = r0.equals(r3);
        if (r0 == 0) goto L_0x0026;
    L_0x007b:
        r0 = "XmlFileGrade";
        r3 = "parseAccountsFromXml account add ";
        com.huawei.hwid.core.d.b.e.e(r0, r3, r4);
        r2.add(r1);
        r0 = new com.huawei.hwid.core.datatype.HwAccount;
        r0.<init>();
        goto L_0x0027;
    L_0x008b:
        return r2;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.huawei.hwid.core.d.a.d.a(org.xmlpull.v1.XmlPullParser, android.content.Context, boolean):java.util.ArrayList<com.huawei.hwid.core.datatype.HwAccount>");
    }

    public void a(Context context, int i, int i2) {
        if (i >= i2) {
            e.d("XmlFileGrade", "newVersion is less then oldVersion, onUpgrade error", true);
            return;
        }
        a(context);
        b(context);
    }

    private void a(Context context) {
        e.b("XmlFileGrade", "update HwAccounts.xml when version update", true);
        a a = a.a(context);
        a.a("last_head_picture_url");
        e.b("XmlFileGrade", "delete last_head_picture_url in HwAccount.xml", true);
        a.a("DEVID_1");
        e.b("XmlFileGrade", "delete DEVID in HwAccount.xml", true);
        a.a("SUBDEVID");
        e.b("XmlFileGrade", "delete SUBDEVID in HwAccount.xml", true);
        a.a("hasEncryptHeadPictureUrl");
        e.b("XmlFileGrade", "delete hasEncryptHeadPictureUrl in HwAccount.xml", true);
        Object a2 = a.a("accessToken", "");
        if (!TextUtils.isEmpty(a2)) {
            a2 = c.a(context, a2);
            if (TextUtils.isEmpty(a2)) {
                a.a("accessToken");
                e.b("XmlFileGrade", "accessToken ecb decrypt error", true);
            } else {
                e.b("XmlFileGrade", "update accessToken in HwAccount.xml", true);
                a.b("accessToken", a2);
            }
        }
        a2 = a.a("UUID", "");
        if (TextUtils.isEmpty(a2)) {
            a.a("UUID");
            e.b("XmlFileGrade", "uuid ecb decrypt error", true);
            return;
        }
        e.b("XmlFileGrade", "update uuid in HwAccount.xml", true);
        a.b("UUID", a2);
    }

    private void b(Context context) {
        if (!b.g(context)) {
            List a;
            if (c(context)) {
                a = a("accounts.xml", context, false);
            } else {
                a = a("accounts.xml", context, true);
            }
            g.a(context, "accounts.xml");
            com.huawei.hwid.c.a.b.a(context, "accounts.xml", a, true);
        }
    }

    private boolean c(Context context) {
        if (!TextUtils.isEmpty(g.b(context, "encryptversion")) || b.a(context, "isSDKAccountDataEncrypted", false)) {
            return false;
        }
        return true;
    }
}
